/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-10-07 14:12:37
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-10-07 15:05:30
 * @FilePath: /sjf/2024-project/2024_centos/test_to_c/sql_ha_test/two_list.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * 
 */
#include "two_list.h"

//初始化
list_node* list_init()
{
    list_node* head = (STLDataType*)malloc(sizeof(STLDataType));
    head->_data = 0;
    head->_next = head;
    head->_prev = head;
}

//创建节点
list_node* create_node(STLDataType x)
{
    list_node* newnode = (STLDataType*)malloc(sizeof(STLDataType));
    if(newnode == NULL)
        exit(-1);
    newnode->_data = x;
    newnode->_next = newnode;
    newnode->_prev = newnode;   
    return newnode;
}

void destory(list_node* head)
{
    list_node* cur = head->_next;//第一个节点
    list_node* next = cur->_next;//cur后一个节点
    while(cur != head)
    {
        free(cur);
        cur = next;
        next = next->_next;
    }
    free(head);
}

void list_print(list_node* head)
{
    list_node* cur = head->_next;
    while(cur != head)
    {
        printf("%d",cur->_data);
        cur = cur->_next;
    }
}

list_node* find_node(list_node* head,STLDataType x)
{
    list_node* cur = head->_next;
    while(cur != head)
    {
        if(cur->_data == x)
            return cur;
        cur = cur->_next;
    }
    return NULL;
}

/**
 *  pos->_prev pos
 *  pos->_prev newnode pos
 */
void list_push(list_node* pos,STLDataType x)
{
    list_node* newnode = create_node(x);
    list_node* cur = pos->_prev;
    cur->_next = newnode;
    newnode->_prev = cur;
    newnode->_next = pos;
    pos->_prev = newnode;
}

/**
 * prev pos next
 */
void list_erase(list_node* pos)
{
    list_node* prev = pos->_prev;
    list_node* next = pos->_next;
    prev->_next = next;
    next->_prev = prev;
    free(pos);
}

int list_size(list_node* head)
{
    int count = 0;
    list_node* cur = head->_next;
    while(cur != head)
    {
        count++;
        cur = cur->_next;
    }
    return count;
}